//
//  LRJDesSecCityViewController.m
//  BreadTravel
//
//  Created by 李润嘉 on 15/12/25.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LRJDesSecCityViewController.h"
#import "LRJDesSecCityModel.h"
#import "ZJDesThirdMorePhotoViewController.h"
#import "ZJDesThirdTravelViewController.h"
#import "GoodCJTViewController.h"


@interface LRJDesSecCityViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *myTableView;
@property (nonatomic, strong)UIImageView *headImageView;
@property (nonatomic, strong)LRJDesSecCityModel *desDetailModel;
@property(nonatomic,copy)NSString *urlStr;
@property(nonatomic,retain)UILabel *nameLabel;
@property(nonatomic,retain)UILabel *goLabel;
@property(nonatomic,retain)UILabel *likeLabel;
@property(nonatomic,copy)NSString *photo;


@end

@implementation LRJDesSecCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.title = self.name_orig;
    
    UIImage *leftImage = [UIImage imageNamed:@"btn_nav_back@2x.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
     [self getData];
    
    //导航栏半透明效果
//    self.navigationController.navigationBar.translucent = YES;
    //是否根据按所在界面的navigationbar与tabbar的高度
    [self setAutomaticallyAdjustsScrollViewInsets:NO];

    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];

    
    //设置tableview的contentView距离上边界200
    //相对于0点,已经向下偏移了-200
    self.myTableView.contentInset = UIEdgeInsetsMake(300.0 , 0, 0, 0);
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;

    
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self createView];
}
//返回
-(void)leftButtonAction:(UIBarButtonItem *)bar{
    
    [self.navigationController popToRootViewControllerAnimated:NO];
    
}
#pragma mark -- 请求网络数据
- (void)getData{
    
    
    
      self.urlStr = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@",self.type,self.id];
   
    [AFNetworkTool getUrl:self.urlStr body:nil result:HXJSON headerFile:nil success:^(id result) {
        NSMutableDictionary *dic = result;
        self.photo = [[[dic objectForKey:@"hottest_places"] objectAtIndex:0] objectForKey:@"photo"];
        self.desDetailModel = [LRJDesSecCityModel modelWithDictionary:dic];
        self.desDetailModel.photo = self.photo;
        self.desDetailModel.name_orig = self.name_orig;
        self.desDetailModel.wish_to_go_count = self.wish_to_go_count;
        self.desDetailModel.visited_count = self.visited_count;
        
        NSURL *url = [NSURL URLWithString:self.desDetailModel.photo];
        [self.headImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"gotravel.jpg"]];

        
        
        
        
        [self.myTableView reloadData];
        
        
    } failure:^(NSError *error) {
       
        NSLog(@"error = %@", error);
    }];
}
- (void)createView{
    
    //相对于0点,图片坐标应该是(0,-200)
    self.headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -300.0, WIDTH, 300)];
    self.headImageView.layer.masksToBounds = YES;
    //设置imageView高度改变时宽度也跟着改变
    self.headImageView.contentMode = UIViewContentModeScaleAspectFill;
     self.headImageView.userInteractionEnabled = YES;
    NSURL *url = [NSURL URLWithString:self.desDetailModel.photo];
    [self.headImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"gotravel.jpg"]];
    [self.myTableView addSubview:self.headImageView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self.headImageView addGestureRecognizer:tap];
    
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, _headImageView.W, 30)];
    nameLabel.text = self.name_orig;
    nameLabel.font = [UIFont systemFontOfSize:25];
    nameLabel.textColor = [UIColor whiteColor];
    [self.headImageView addSubview: nameLabel];
    
    UILabel *smallLabel = [[UILabel alloc] initWithFrame:CGRectMake(nameLabel.X, nameLabel.Y + nameLabel.H + 5, _headImageView.W, 20)];
    smallLabel.text = [NSString stringWithFormat:@"%@ 去过 / %@ 喜欢", self.visited_count, self.wish_to_go_count];
    smallLabel.textColor = [UIColor whiteColor];
    smallLabel.alpha = 0.8;
    smallLabel.font = [UIFont systemFontOfSize:12];
    [self.headImageView addSubview:smallLabel];
    
    
    
}
- (void)tapAction:(UITapGestureRecognizer *)tap{
    
     ZJDesThirdMorePhotoViewController *zjVC = [[ZJDesThirdMorePhotoViewController alloc] init];
    [self.navigationController pushViewController:zjVC animated:YES];
    
    zjVC.urlStr = self.urlStr;
    zjVC.type = self.type;
    zjVC.id = self.id;
    
}

//指定多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    
    if (indexPath.row == 0) {
        UIImageView *myImage = [[UIImageView alloc] init];
        myImage.image = [UIImage imageNamed:@"lvxingdi.png"];
        cell.imageView.image = myImage.image;
        
       
        
    }
    else
    {
        UIImageView *myImage = [[UIImageView alloc] init];
        myImage.image = [UIImage imageNamed:@"youji.png"];
        cell.imageView.image = myImage.image;

    }
    
    return cell;
}
//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //刚开始y的偏移量初始值就是-264
//    NSLog(@"y1 === %f",scrollView.contentOffset.y);
    CGFloat y = scrollView.contentOffset.y ;//加上导航栏高度,第一次是-200
//    NSLog(@"y2 === %f",y);
    
    if (y < -300) {
        CGRect frame = self.headImageView.frame;
        frame.origin.y = y;//偏移了多少,
        frame.size.height =  -y;//tablview向下偏移了多少,高度就是多少
        self.headImageView.frame = frame;
    }
    
}

#pragma mark -- 点击cell触发
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        ZJDesThirdTravelViewController *zjDesThirdTraVC = [[ZJDesThirdTravelViewController alloc] init];
        zjDesThirdTraVC.type = self.type;
        zjDesThirdTraVC.id = self.id;
        [self.navigationController pushViewController:zjDesThirdTraVC animated:YES];
    }
    else
    {
        GoodCJTViewController *goodVC = [[GoodCJTViewController alloc] init];
        [self.navigationController pushViewController:goodVC animated:YES];
        goodVC.type = self.type;
        goodVC.id = self.id;
        
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
